package cn.com.haopy.yue.ai.core.config.wx;


import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaMsgServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WxConfig {
    @Autowired
    private WxMaProperties wxMaProperties;


    @Bean
    public WxMaConfig wxMaConfig() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(wxMaProperties.getAppId());
        config.setSecret(wxMaProperties.getSecret());
        return config;
    }


    @Bean
    public WxMaService wxMaService(WxMaConfig wxMaConfig) {
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(wxMaConfig);
        return service;
    }

    @Bean(name = "wxMaPayConfig")
    public WxPayConfig wxMaPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wxMaProperties.getAppId());
        payConfig.setMchId(wxMaProperties.getMchId());
        payConfig.setMchKey(wxMaProperties.getMchKey());
        payConfig.setNotifyUrl(wxMaProperties.getNotifyUrl());
        payConfig.setKeyPath(wxMaProperties.getKeyPath());
        payConfig.setTradeType("JSAPI");
        payConfig.setSignType("MD5");
        return payConfig;
    }


    @Bean("wxMaPayService")
    public WxPayService wxMaPayService(@Qualifier("wxMaPayConfig") WxPayConfig wxMaPayConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxMaPayConfig);
        return wxPayService;
    }


    @Bean
    public WxMaMsgService wxMaMsgService(WxMaService wxMaService) {
        WxMaMsgService wxMaMsgService = new WxMaMsgServiceImpl(wxMaService);
        return wxMaMsgService;
    }
}
